package com.example.zhongxiangmo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.text.ParsePosition;
import java.util.ArrayList;

/**
 * Created by 文龙 on 2019/12/29.
 */

public class DuoBuJuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<DuoBuBean.DataBean> list;
    private int ITEM_TYPE_ONE = 1;
    private int ITEM_TYPE_TW0 = 2;
    private ItemClickListener itemClickListener;
    private ItemLongClickListener itemLongClickListener;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setItemLongClickListener(ItemLongClickListener itemLongClickListener) {
        this.itemLongClickListener = itemLongClickListener;
    }

    public DuoBuJuAdapter(Context context, ArrayList<DuoBuBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_ONE) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_duobuju1, null);

            ViewHolderOne viewHolderOne = new ViewHolderOne(inflate);
            return viewHolderOne;
        } else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_duobuju2, null);
            ViewHolderTwo viewHolderTwo = new ViewHolderTwo(inflate);
            return viewHolderTwo;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if (getItemViewType(position) == ITEM_TYPE_ONE) {
            ViewHolderOne viewHolderOne = (ViewHolderOne) holder;
            viewHolderOne.mTvDuobuju.setText(list.get(position).getTitle());
            RequestOptions requestOptions = new RequestOptions();
            requestOptions.circleCrop();

            Glide.with(context).load(list.get(position).getPic()).apply(requestOptions).into(viewHolderOne.mIvDuobuju);

        } else {
            ViewHolderTwo viewHolderTwo = (ViewHolderTwo) holder;
            viewHolderTwo.mTvDuobuju.setText(list.get(position).getTitle());
            RequestOptions requestOptions = new RequestOptions();
            requestOptions.circleCrop();
            Glide.with(context).load(list.get(position).getPic()).apply(requestOptions).into(viewHolderTwo.mIvDuobuju);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.ItemClick(position);
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                itemLongClickListener.ItemLongClick(position);
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return ITEM_TYPE_ONE;
        } else {
            return ITEM_TYPE_TW0;
        }
    }

    static class ViewHolderOne extends RecyclerView.ViewHolder {
        View view;
        ImageView mIvDuobuju;
        TextView mTvDuobuju;

        ViewHolderOne(View view) {
            super(view);
            this.view = view;
            this.mIvDuobuju = (ImageView) view.findViewById(R.id.iv_duobuju);
            this.mTvDuobuju = (TextView) view.findViewById(R.id.tv_duobuju);
        }
    }

    static class ViewHolderTwo extends RecyclerView.ViewHolder{
        View view;
        TextView mTvDuobuju;
        ImageView mIvDuobuju;

        ViewHolderTwo(View view) {
            super(view);
            this.view = view;
            this.mTvDuobuju = (TextView) view.findViewById(R.id.tv_duobuju);
            this.mIvDuobuju = (ImageView) view.findViewById(R.id.iv_duobuju);
        }
    }

    public interface ItemClickListener{
        void ItemClick(int i);
    }
    public interface ItemLongClickListener{
        void ItemLongClick(int i);
    }

}
